// base
import Taro, { Component } from '@tarojs/taro'
import { View, Text } from '@tarojs/components'
// redux
import { connect } from '@tarojs/redux'
import { DOREQUESTCOMPANYHOMEPAGE } from './store/actions/pagecompanyhomepage'
import { UPDATEPAGECOMPANYHOMEPAGE } from './store/types/pagecompanyhomepage'
// less
import './index.less'
import SwiperVertical from './components/swiper-vertical'
import CompanyIntro from './components/company-intro'
import ChooseBar from './components/choose-bar'
import CompanyIntroDetail from './components/company-intro-detail'

const mapActiontoProps = dispatch => ({
  updateState (payload) {
    dispatch({
      type: UPDATEPAGECOMPANYHOMEPAGE,
      payload: {
        ...payload
      }
    })
  },
  dorequest (payload) {
    return new Promise(resolve => {
      let promise = DOREQUESTCOMPANYHOMEPAGE(payload)
      dispatch(promise)
      promise.payload.then(data => {
        resolve(data)
      })
    })
  }
})

@connect(
  ({ pagecompanyhomepage, pagejobdetail }) => ({
    ...pagecompanyhomepage,
    jobtopdetail: pagejobdetail.jobtopdetail
  }),
  mapActiontoProps
)
class CompanyHomepage extends Component {
  config = {}

  constructor (props) {
    super(props)
    this.state = {
      jobDetail: {}
    }
  }

  componentWillMount () {
    // onLoad
    this.querywrap()
  }

  componentDidMount () {
    // onready
    this.dealJobDetail()
  }

  componentDidShow () {
    // onshow
  }

  componentDidHide () {
    // onhide
  }

  componentWillUnmount () {
    // onUnload
  }

  dealJobDetail = () => {
    const { jobtopdetail } = this.props
    let jobDetail = {}
    jobDetail.companyLogo =
      'https://imagesize.hrwork.com/sp/files/aba62394-93b2-4db4-a2b7-dbf19e160f64.png'
    jobDetail.companyName = jobtopdetail.companyName
    this.setState({
      jobDetail
    })
  }

  querywrap = () => {
    const params = this.$router && this.$router.params
    if (params.pagetype != 'preview') {
      this.props.updateState({
        companydetail: {},
        companyintro: '',
        companyaddress: {},
        swipersource: []
      })
      this.props.dorequest({
        companyId: params && params.id * 1
      })
    }
  }

  render () {
    const { jobDetail } = this.state
    const {
      swipersource,
      companydetail,
      jobcount,
      choosecurrent,
      updateState,
      companyintro,
      companyaddress,
      iacmessage
    } = this.props
    return (
      <View className="CompanyHomepage_id58732">
        <SwiperVertical dataSource={swipersource} />
        <CompanyIntro detail={JSON.stringify(companydetail) == '{}' ? jobDetail : companydetail} />
        <ChooseBar jobcount={jobcount} current={choosecurrent} onUpdateState={updateState} />
        <CompanyIntroDetail
          visible={choosecurrent}
          companyintro={companyintro}
          companyaddress={companyaddress}
          iacmessage={iacmessage}
        />
        {/* <JobWrap visible={this.props.choosecurrent} dataSource={this.props.joblist}></JobWrap> */}
      </View>
    )
  }
}

export default CompanyHomepage
